package cn.cmkj.auction.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.util.LogUtil;
import org.xutils.http.HttpMethod;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import cn.cmkj.auction.R;
import cn.cmkj.auction.adapter.FavShuAdapter;
import cn.cmkj.auction.adapter.MainShuAdapter;
import cn.cmkj.auction.app.BaseActivity;
import cn.cmkj.auction.app.Urls;
import cn.cmkj.auction.data.db.User;
import cn.cmkj.auction.data.http.Shuhua;
import cn.cmkj.auction.utils.HttpCallback;
import cn.cmkj.auction.utils.ItemClickSupport;
import cn.cmkj.auction.widget.swipeRecyclerView.MySwipeMenuRecyclerView;

/**
 * Created by cunguoyao on 2018/1/19.
 */
@ContentView(R.layout.activity_goods_list_by_tag)
public class MyFavListActivity extends BaseActivity implements MySwipeMenuRecyclerView.OnLoadListener, View.OnClickListener {

    @ViewInject(R.id.title_back)
    private ImageButton titleBack;
    @ViewInject(R.id.title_text)
    private TextView titleText;
    @ViewInject(R.id.swipeRecyclerView)
    private MySwipeMenuRecyclerView swipeRecyclerView;

    private List<Shuhua> mData;
    //private FavShuAdapter mainShuAdapter;
    private MainShuAdapter mainShuAdapter;
    private boolean requesting = false;
    private int page = 1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        mData = new ArrayList<>();
        mainShuAdapter = new MainShuAdapter(this, mData);
        swipeRecyclerView.getRecyclerView().setLayoutManager(new GridLayoutManager(this, 2));
        //((SwipeMenuRecyclerView)swipeRecyclerView.getRecyclerView()).setLongPressDragEnabled(true); // 开启拖拽。
        //((SwipeMenuRecyclerView)swipeRecyclerView.getRecyclerView()).setItemViewSwipeEnabled(true); // 开启滑动删除。
        swipeRecyclerView.setAdapter(mainShuAdapter);
        ItemClickSupport.addTo(swipeRecyclerView.getRecyclerView()).setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
            @Override
            public void onItemClicked(RecyclerView recyclerView, View itemView, int position) {
                Shuhua shuhua = mData.get(position);
                Intent intent = new Intent(MyFavListActivity.this, ShuhuaDetailActivity.class);
                intent.putExtra("shuhua", shuhua);
                startActivity(intent);
            }
        });
        swipeRecyclerView.setOnLoadListener(this);
        swipeRecyclerView.setRefreshing(true);
        titleBack.setOnClickListener(this);
        titleText.setText("我的收藏");
    }

    @Override
    public void onRefresh() {
        if(!requesting) {
            page = 1;
            mData.clear();
            mainShuAdapter.notifyDataSetChanged();
            fetchData();
        }
    }

    @Override
    public void onLoadMore() {
        if(!requesting) {
            page = page +1;
            LogUtil.e("--onLoadMore:page=" + page);
            fetchData();
        }
    }

    public void fetchData() {
        User user = getAccount();
        if(user == null) {
            showLoginDialog(this);
        }else {
            requesting = true;
            RequestParams params = new RequestParams(Urls.api);
            params.addParameter("method", Urls.favList);
            params.addParameter("page_no", page);
            params.addParameter("page_size", Urls.pageSize);
            params.addParameter("accesstoken", user.getToken());
            params.addParameter("member_id", user.getId());
            x.http().request(HttpMethod.POST, params, new HttpCallback(this) {
                @Override
                public void success(String s) {
                    requesting = false;
                    swipeRecyclerView.complete();
                    JSONObject jsonObject = null;
                    try {
                        jsonObject = new JSONObject(s);
                        JSONObject json = jsonObject.optJSONObject("data");
                        JSONArray jsonArray = json.optJSONArray("goods");
                        List<Shuhua> temp = Shuhua.parseJsonArrayForFav(jsonArray);
                        mData.addAll(temp);
                        mainShuAdapter.notifyDataSetChanged();
                        if (Urls.pageSize > temp.size()) {
                            swipeRecyclerView.setLoadMoreEnable(false);
                        } else {
                            swipeRecyclerView.setLoadMoreEnable(true);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void error(Throwable ex, String msg, boolean isOnCallback) {
                    requesting = false;
                    swipeRecyclerView.complete();
                    if (ex != null) ex.printStackTrace();
                    LogUtil.e("---onError-----" + "onError");
                    Toast.makeText(MyFavListActivity.this, TextUtils.isEmpty(msg) ? "请求失败" : msg, Toast.LENGTH_SHORT).show();
                }

                @Override
                public void cancel(CancelledException cex) {
                    requesting = false;
                    swipeRecyclerView.complete();
                    LogUtil.e("---onCancelled-----" + "onCancelled");
                }

                @Override
                public void finished() {
                    requesting = false;
                    swipeRecyclerView.complete();
                }
            });
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_back:
                finish();
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
